//招聘者模块

import { getRectuiterBasicInfo } from "@/api/login";

export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('recruiterToken') || null,
    tempToken: localStorage.getItem('recruiterTempToken') || null,
    //用户信息
    userInfo: JSON.parse(localStorage.getItem('recruiterUserInfo')) || {},
    companyInfo: JSON.parse(localStorage.getItem('recruiterCompanyInfo')) || {},
    jobpostingstatus: 'VERIFY_COMPANY_REVIEW',
    failedMessage: '',
    Greeting: '您好～ 您的过往经历很符合我们的需求，约个面试详谈？'

  },
  mutations: {
    //设置token
    SET_TOKEN(state, token) {
      state.token = token;
      localStorage.setItem('recruiterToken', token);
    },

    //设置临时token
    SET_TEMP_TOKEN(state, tempToken) {
      state.tempToken = tempToken;
      localStorage.setItem('recruiterTempToken', tempToken);
    },

    //设置基本信息
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo;
      localStorage.setItem('recruiterUserInfo', JSON.stringify(userInfo));
    },

    //设置公司信息
    SET_COMPANY_INFO(state, companyInfo) {
      state.companyInfo = companyInfo;
      localStorage.setItem('recruiterCompanyInfo', JSON.stringify(companyInfo));
    },

    //清理状态
    CLEAR_STATE(state) {
      state.token = null;
      state.tempToken = null;
      state.userInfo = {};
      state.companyInfo = {};
      // 修复：分别删除每个 localStorage 项
      localStorage.removeItem('recruiterToken');
      localStorage.removeItem('recruiterTempToken');
      localStorage.removeItem('recruiterUserInfo');
      localStorage.removeItem('recruiterCompanyInfo');
    },


    SET_REVIEW_COMPANY_INFO(state, info) {
      state.reviewCompanyInfo = info
    },

    SET_JOBPOSTING_STATUS(state, status) {
      state.jobpostingstatus = status
    },
    SET_GREETING(state, Greeting) {
      state.Greeting = Greeting
    }
  },
  actions: {
    login({ commit }, { token, companyInfo }) {
      commit('SET_TOKEN', token);
      commit('SET_USER_INFO', companyInfo);
    },



    //获取用户信息
    async fetchRectuiterInfo({ state, commit }) {
      if (!state.token) {
        return false;
      }

      try {
        const response = await getRectuiterBasicInfo();

        if (response.data.code === 201 && response.data.data.userId) {
          commit('SET_USER_INFO', response.data.data);
          return true;
        } else {
          throw new Error(response.data.message || '用户信息获取失败');
        }
      } catch (error) {
        commit('CLEAR_STATE');
        return false;
      }
    },

    // 初始化认证状态
    async initAuth({ state, commit }) {
      if (state.userInfo.userId) {
        console.log('已招聘者信息')
        return state.userInfo
      }
      const token = localStorage.getItem("recruiterToken");
      if (token) {
        commit("SET_TOKEN", token);
        try {
          const response = await getRectuiterBasicInfo();
          commit("SET_USER_INFO", response.data.data);
        } catch (error) {
          commit("CLEAR_STATE");
        }
      }
    },

    logout({ commit }) {
      commit('CLEAR_STATE');
    }
  },
  getters: {
    userInfo(state) {
      return state.userInfo
    },
    reviewFailedMsg(state) {
      return state.failedMessage;
    }
  }
};